package com.cloudptt.api.product.adapter;

import java.util.Iterator;
import java.util.List;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.comm.Const;
import com.cloudptt.api.models.CallDescInfo;
import com.cloudptt.api.pjsip.service.AppRTCAudioManager;
import com.cloudptt.api.product.IGetReportData;
import com.cloudptt.api.product.ISuperCallOperator;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.CallListener;
import com.cloudptt.api.product.func.call.group.HalfCallInfo;
import com.cloudptt.api.product.func.call.group.GroupCallSpeakListener;
import com.cloudptt.api.product.func.call.group.fsm.call.GroupCallAgent;
import com.cloudptt.api.product.func.call.group.fsm.video.GroupCallMemberInfo;
import com.cloudptt.api.product.func.call.single.CallInfo;
import com.cloudptt.api.product.func.media.IEventAdapter;
import com.cloudptt.api.product.func.media.light.LightManager;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.AddrOperator;
import com.cloudptt.api.product.operator.CallOperator;
import com.cloudptt.api.product.res.Resource;
import com.cloudptt.api.product.ringer.PromptTone;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.GroupInfo;
import com.mcptt.db.model.UserInfo;
import com.mcptt.config.LoginInfoStruct;

import android.content.Context;
import android.view.KeyEvent;

public class GCallEventAdapter implements IEventAdapter,CallListener,GroupCallSpeakListener
{
	private static String THIS_FILE = "GCallEventAdapter";
	private Context ctx = null;
	protected Product product = null;
	protected ISuperCallOperator callOperator = null;
	private LightManager lightManager = null;
	private String curCallKey = null;
	private int callState = CallConst.CallState_InitState;
	private int floorState = CallConst.FloorState_Null;
	private AddrInfo floorAddrinfo = null;
	private ICallInfoDesc desc = null;
	private CallDescInfo callDescInfo = null;

	private CallListener callListener = null;
	private GroupCallSpeakListener floorListener = null;

	private boolean isGroup = true;
	private IGetReportData reportDate = null;

	public void setCallInfoDesc(ICallInfoDesc desc) {
		Log.i(THIS_FILE, "base setCallInfoDesc desc = " + desc);
		this.desc = desc;
		if(desc != null
				&& curCallKey != null
				&& callOperator != null)
		{
			HalfCallInfo callInfo = callOperator.getHalfCallInfo(curCallKey);
			Log.i(THIS_FILE, "base setCallInfoDesc callInfo = " + callInfo);
			if(callInfo != null
					&& callInfo.getCallAttr() != null)
			{
				boolean hasVideo = callInfo.getCallAttr().isHasVideo();
				Log.i(THIS_FILE, "base setCallInfoDesc hasVideo = " + hasVideo);
				if(!hasVideo) {
					updateCallDescByCallInfo(curCallKey,callInfo);
					updateCallState(curCallKey, callInfo.getCallState(), hasVideo);
				}
			}
			else
			{
				updateCallState(curCallKey,CallConst.CallState_InitState,false);
			}
		}
	}

	public void setHalfCallListener(CallListener callListener,GroupCallSpeakListener floorListener)
	{
		this.callListener = callListener;
		this.floorListener = floorListener;
	}


	public void setCurCallKey(String callKey) {
		Log.i(THIS_FILE, "setCurCallKey callKey = " + callKey);
		this.curCallKey = callKey;
	}


	public GCallEventAdapter(Context ctx, Product product, IGetReportData reportDate)
	{
		this.ctx = ctx;
		lightManager = new LightManager(ctx);
		this.product = product;
		this.callOperator = product.getSuperCallOperator();
		this.callDescInfo = new CallDescInfo();
		this.reportDate = reportDate;
	}

	public void clearResource()
	{
		if(lightManager != null)
		{
			lightManager.clearResouce();
			lightManager = null;
		}
	}

	@Override
	public void onPtt(int type,boolean isDefaultGroup,boolean hasVideo,boolean isRing,boolean isEmergency) {
		// TODO Auto-generated method stub
		pttBtnAction(type,isDefaultGroup,hasVideo,isRing,isEmergency);
	}

	@Override
	public void onFreq(int index) {
		// TODO Auto-generated method stub
		Log.i(THIS_FILE, "onFreq:" + "index:" + index);
		if(ctx == null)
		{
			Log.e(THIS_FILE, "onLogin ctx is null");
		}
		if(product == null)
		{
			Log.e(THIS_FILE, "onFreq product is null");
		}

		if(ctx == null)
		{
			return;
		}

		AddrOperator operator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
		final List<GroupInfo> grouplist = operator.getGroupList();

		Log.d(THIS_FILE, "onFreqAction channel: = " + index);
		if(index < 0
				|| index >= grouplist.size())
		{
			return;
		}

		GroupInfo info = null;
		if (index <= grouplist.size()
				&& grouplist.size() > 0
				&& index > 0)
		{
			info = grouplist.get(index);
		}
		AddrInfo curInfo = product.getCurGroupInfo();
		int result = operator.checkOperate();
		if(CauseCont.OperateCheck_Suc != result)
		{
			operator.startSoundPlayer(PromptTone.Type_Disconnect);
		}

		if(curInfo != null
				&& !curInfo.isSameOne(info))
		{
			hangUpGroupCall();
		}

		product.setDefaultGroupInfo(info);
	}

	@Override
	public void onUpDownFreq(int type) {
		Log.e(THIS_FILE, "onUpDownFreq  type:" + type);


		if(product == null)
		{
			return;
		}
		AddrOperator operator = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
		final List<GroupInfo> grouplist = operator.getGroupList();

		int targetindex = 0;
		AddrInfo curInfo = product.getCurGroupInfo();

		if(curInfo != null
				&& grouplist != null)
		{
			for(int i=0;i<grouplist.size();i++)
			{
				if(curInfo.isSameOne(grouplist.get(i)))
				{
					targetindex = i;
				}
			}
		}
		Log.e(THIS_FILE, "eventAdapter.onUpDownFreq(Const.FREQ_UP);" + targetindex);

		if(type==Const.FREQ_UP)
		{

			if(targetindex==0)
			{
				targetindex= grouplist.size()-1;
			}
			else
			{
				targetindex= targetindex-1;
			}
		}
		else if(type==Const.FREQ_DOWN)
		{
			if(targetindex==grouplist.size()-1)
			{
				targetindex= 0;
			}
			else
			{
				targetindex= targetindex+1;
			}
		}
		else
		{
			return;
		}

		Log.e(THIS_FILE, "onUpDownFreq 2 targetindex:" + targetindex);

		if(targetindex >=0
				&& targetindex <grouplist.size())
		{
			GroupInfo info = grouplist.get(targetindex);

			int result = operator.checkOperate();
			if(CauseCont.OperateCheck_Suc != result)
			{
				operator.startSoundPlayer(PromptTone.Type_Disconnect);
			}
			if(curInfo != null
					&& !curInfo.isSameOne(info))
			{
				hangUpGroupCall();
			}

			product.setDefaultGroupInfo(info);
		}

	}



	@Override
	public void onUserDefined1(boolean isLongPress) {
		// TODO Auto-generated method stub
		Log.i(THIS_FILE, "onUserDefined1:" + "isLongPress:" + isLongPress );
		if(ctx == null)
		{
			Log.e(THIS_FILE, "onUserDefined1 ctx is null");
		}
		if(product == null)
		{
			Log.e(THIS_FILE, "onUserDefined1 product is null");
		}
		if(ctx == null)
		{
			return;
		}

		if(isLongPress)
		{
			if(lightManager != null)
			{
				lightManager.openLED(false, LightManager.LED_BLUE,LightManager.Level_Operate);
			}
		}

	}



	@Override
	public void onUserDefined3(boolean isLongPress) {
		// TODO Auto-generated method stub
		Log.i(THIS_FILE, "onUserDefined3:" + "isLongPress:" + isLongPress);
		if(ctx == null)
		{
			Log.e(THIS_FILE, "onUserDefined3 ctx is null");
		}
		if(product == null)
		{
			Log.e(THIS_FILE, "onUserDefined3 product is null");
		}
		if(ctx == null)
		{
			return;
		}

		if(!isLongPress)
		{
			onShortUserDefined3Action(curCallKey);
		}
//		else
//		{
//			onLongUserDefined3Action(curCallKey);
//		}
	}


	@Override
	public void onSOS(boolean isLongPress) {
		// TODO Auto-generated method stub
		Log.i(THIS_FILE, "onSOS:" + "isLongPress:" + isLongPress);
		if(ctx == null)
		{
			Log.e(THIS_FILE, "onSOS ctx is null");
		}
		if(product == null)
		{
			Log.e(THIS_FILE, "onSOS product is null");
		}
		if(ctx == null)
		{
			return;
		}
		if(callOperator == null)
		{
			this.callOperator = product.getSuperCallOperator();
		}
//		if(isLongPress)
//		{
//			//			sendMsgBySOS();
//			GroupCallAgent callAgent = (GroupCallAgent)groupCallOperator;
//			CallOperator operator = callAgent.getOperator();
//			operator.startSoundPlayer(R.raw.sos);
//		}
	}



	private void onShortUserDefined3Action(String curCallKey)
	{
		boolean isActiveCall = false;
		if (callOperator != null)
		{
			isActiveCall = callOperator.isActiveCall(curCallKey);
			if(isActiveCall)
			{
//				String str = ctx.getString(R.string.str_hangup);
//				//				product.playVoiceMsg(str);
//				CommDlg.showShortToast(ctx, str);
				callOperator.doUserAction(curCallKey,CallConst.BtnAction_Hangup,"");
			}
		}
	}


//	public void acceptGroupCall()
//	{
//		Log.e(THIS_FILE, "acceptGroupCall");
//		boolean isActiveCall = callOperator.isActiveCall(curCallKey);
//		Log.e(THIS_FILE, "acceptGroupCall isActiveCall = " + isActiveCall);
//		if(isActiveCall)
//		{
//			callOperator.doUserAction(curCallKey,CallConst.BtnAction_Answer,"");
//		}
//	}
	public void hangUpGroupCall()
	{
		boolean isActiveCall = callOperator.isActiveCall(curCallKey);
		Log.e(THIS_FILE, "hangUpGroupCall isActiveCall = " + isActiveCall + " curCallKey = " + curCallKey);
		if(isActiveCall)
		{
			callOperator.doUserAction(curCallKey,CallConst.BtnAction_Hangup,"");
		}
	}
	public boolean isEmergencyMt ()
	{
		HalfCallInfo halfCallInfo = callOperator.getHalfCallInfo(curCallKey);
		CallAttr callAttr = halfCallInfo.getCallAttr();
		AddrInfo info = callAttr.getGroupInfo();
		Log.d(THIS_FILE,"isEmergencyMt info = " + info);
		int callDir = callAttr.getCallDir();
		if(callDir == CallAttr.CallDir_MT
				&& info.isEmergencyNum())
		{
			return true;
		}
		return false;
	}

	public void disconnectWarring(boolean isconnected)
	{
		Log.d(THIS_FILE,"disconnectWarring");
		if (!isconnected)
		{
			if(lightManager != null)
			{
				lightManager.openLED(false,LightManager.LED_RED,LightManager.Level_High);
			}
			GroupCallAgent callAgent = (GroupCallAgent)callOperator;
			CallOperator operator = callAgent.getOperator( );
			operator.startSoundPlayer(PromptTone.Type_Disconnect);
		}
		else
		{
			if(lightManager != null)
			{
				lightManager.closeLED(LightManager.Level_High);
			}
		}
	}

	public void loginWarring(boolean isLogined)
	{
		Log.d(THIS_FILE,"loginWarring isLogined = " + isLogined);
		if(lightManager == null)
		{
			return;
		}
		lightManager.onLogin(isLogined);
		if (!isLogined)
		{
			lightManager.openLED(false,LightManager.LED_RED,LightManager.Level_High);
		}
		else
		{
			lightManager.closeLED(LightManager.Level_High);
		}
	}

	public void soundLinking()
	{
		GroupCallAgent callAgent = (GroupCallAgent)callOperator;
		CallOperator operator = callAgent.getOperator( );
		operator.startSoundPlayer(PromptTone.Type_Linking);
	}

	public void openOperateLED(boolean isFlash, int ledColor)
	{
		if(lightManager != null)
		{
			lightManager.resetLED();
			lightManager.openLED(isFlash,ledColor,LightManager.Level_Operate);
		}
	}
	public void closeOperateLED()
	{
		lightManager.closeLED(LightManager.Level_Operate);
	}

//	public void ringLowWarring()
//	{
//		GroupCallAgent callAgent = (GroupCallAgent)groupCallOperator;
//		CallOperator operator = callAgent.getOperator( );
//		operator.startSoundPlayer(PromptTone.Type_Low);
//	}
//	private void ringNormalWarring(int value)
//	{
//		GroupCallAgent callAgent = (GroupCallAgent)groupCallOperator;
//		CallOperator operator = callAgent.getOperator( );
//		boolean result = operator.checkOperate();
//		if(!result)
//		{
//			operator.startSoundPlayer(PromptTone.Type_Normal);
//		}
//		else
//		{
//			String str = ctx.getString(R.string.battery);
//			str = str + value + "%";
//			//			product.playVoiceMsg(str);
//			CommDlg.showShortToast(ctx, str);
//		}
//	}

	private void pttBtnAction(int type,boolean isDefaultGroup,boolean hasVideo,boolean isRing,boolean isEmergency)
	{
		Log.d(THIS_FILE,"pttBtnAction type = " + type + " isDefaultGroup = " + isDefaultGroup + " hasVideo = " + hasVideo + " curCallKey = " + curCallKey + " isEmergency = " + isEmergency);
		if(ctx == null)
		{
			return;
		}
//		ISuperCallOperator callTracker = (ISuperCallOperator)callOperator;
//		CallOperator operator = callTracker.getOperator( );
//		boolean result = operator.checkOperate();
		AddrInfo info = product.getCurGroupInfo();
//		Log.d(THIS_FILE,"pttBtnAction result = " + result);
		Log.d(THIS_FILE,"pttBtnAction info = " + info);
		if(info == null)
		{
			if(Const.MSG_TALK_ON == type)
			{
				String str = Resource.getString("null_group");//ctx.getString(R.string.null_group);
				CommDlg.showShortToast(ctx, str);
			}
			return;
		}

		ISuperCallOperator  operator = product.getSuperCallOperator();
//		if(operator.isHaveActiveWebrtcCall())
//		{
//			if(Const.MSG_TALK_ON == type)
//			{
//				String str = Resource.getString("have_active_pcall");//ctx.getString(R.string.null_group);
//				CommDlg.showShortToast(ctx, str);
//			}
//			return;
//		}
		if(curCallKey == null)
		{
			curCallKey = product.getCurGroupCallKey();
		}

		boolean canDown = false;
		boolean isActiveCall = false;
		List<CallInfo> singleCallInfos = operator.getSingleCallInfoList();
		Log.d(THIS_FILE,"pttBtnAction singleCallInfos = " + singleCallInfos);
		if(singleCallInfos != null
				&& singleCallInfos.size() > 0)
		{
			String str = Resource.getString("has_call");//ctx.getString(R.string.null_group);
			CommDlg.showShortToast(ctx, str);
			return;
		}

		HalfCallInfo callInfo = operator.getHalfCallInfo(curCallKey);
		Log.d(THIS_FILE,"pttBtnAction callInfo = " + callInfo + " curCallKey = " + curCallKey);
		if(callInfo == null)
		{
			canDown = true;
			isActiveCall = false;
		}
		else
		{
			boolean oldHasVideo = callInfo.isHasVideo();
			int callState = callInfo.getCallState();
			int SpeakState = callInfo.getFloorState();
			int transState = callInfo.getVideoTransState();
			int recvState = callInfo.getVideoRecvState();
			Log.d(THIS_FILE,"pttBtnAction oldHasVideo = " + oldHasVideo + " callState = " + callState + " SpeakState = " + SpeakState);
			if(oldHasVideo == hasVideo)
			{
				canDown = canPttBtnDown(callState,SpeakState,transState,recvState);
				isActiveCall = isActiveCall(callState);
			}
			else
			{
				canDown = false;
				isActiveCall = true;
				String str = Resource.getString("group_has_call");//ctx.getString(R.string.null_group);
				CommDlg.showShortToast(ctx, str);
			}

		}

		if (type == Const.MSG_TALK_ON
				&& canDown)
		{
			int result = CauseCont.OperateErr_Exception;
			if(!isActiveCall)
			{
				AddrInfo groupInfo = product.getCurGroupInfo();
				UserInfo loginUserInfo = product.getMcpttLoginInfo().getMcIdInfo().toUserInfo();
				String locationStr = "";
				if(reportDate != null
						&& reportDate.getReportData() != null)
				{
					locationStr = reportDate.getReportData().toString();
				}
				CallAttr callAttr = new CallAttr(groupInfo,loginUserInfo,CallAttr.CallDir_MO,isGroup,hasVideo,isEmergency,"",locationStr);
				result = callOperator.newCall(callAttr);
				Log.d(THIS_FILE,"pttBtnAction result = " + result);
				if(CauseCont.OperateCheck_Suc == result)
				{
//					if(isDefaultGroup)
					{
						this.curCallKey = callAttr.getCallKey();
						Log.d(THIS_FILE,"pttBtnAction curCallKey = " + curCallKey);
					}
					result = callOperator.doUserAction(curCallKey,CallConst.BtnAction_Make,"");
					String errStr = CauseCont.getOperateCheckErrStr(result, ctx);
					CommDlg.showToast(ctx, errStr);
				}
				else
				{
					String errStr = CauseCont.getOperateCheckErrStr(result,ctx);
					CommDlg.showToast(ctx,errStr);
					return;
				}
			}
			else
			{
				result = callOperator.doUserAction(curCallKey,CallConst.BtnAction_Request,"");
			}
			if(CauseCont.OperateCheck_Suc == result)
			{
				callOperator.startSoundPlayer(PromptTone.Type_On);
			}
			else if(CauseCont.OperateErr_NoAgent != result)
			{
				String errStr = CauseCont.getOperateCheckErrStr(result, ctx);
				CommDlg.showToast(ctx, errStr);
			}

		} else if (type == Const.MSG_TALK_OFF)
		{
//			int isSuc = CauseCont.OperateErr_Exception;
//			if(!isActiveCall)
//			{
//				isSuc = callOperator.doUserAction(curCallKey,CallConst.BtnAction_Hangup);
//			}
//			else
//			{
//				isSuc = callOperator.doUserAction(curCallKey,CallConst.BtnAction_Release);
//			}
			HalfCallInfo gCallInfo = callOperator.getHalfCallInfo(curCallKey);
			Log.d(THIS_FILE,"pttBtnAction gCallInfo = " + gCallInfo);
//			if(gCallInfo != null
//					&& gCallInfo.getSpeakState() != CallConst.FloorState_Permission)
//			{
//				return;
//			}
			int isSuc = callOperator.doUserAction(curCallKey,CallConst.BtnAction_Release,"");
			if(CauseCont.OperateCheck_Suc == isSuc)
			{
				if(isRing) {
					callOperator.startSoundPlayer(PromptTone.Type_Off);
				}
			}
			else if(CauseCont.OperateErr_OpQuick == isSuc)
			{
				String str = Resource.getString("operator_quick");
				CommDlg.showShortToast(ctx, str);
			}
			else if(CauseCont.OperateErr_NoAgent != isSuc)
			{
				String errStr = CauseCont.getOperateCheckErrStr(isSuc, ctx);
				CommDlg.showToast(ctx, errStr);
			}
		}
	}

	public void doReleaseFloor()
	{
		int isSuc = callOperator.doUserAction(curCallKey,CallConst.BtnAction_Release,"");
		if(CauseCont.OperateCheck_Suc == isSuc)
		{
			callOperator.startSoundPlayer(PromptTone.Type_Off);
		}
		else if(CauseCont.OperateErr_OpQuick == isSuc)
		{
			String str = Resource.getString("operator_quick");
			CommDlg.showShortToast(ctx, str);
		}
		else if(CauseCont.OperateErr_NoAgent != isSuc)
		{
			String errStr = CauseCont.getOperateCheckErrStr(isSuc, ctx);
			CommDlg.showToast(ctx, errStr);
		}
	}

	public void subcribeConference()
	{
		Log.d(THIS_FILE, "subcribeConference");
		callOperator.subcribeConference(curCallKey);
	}

	public void soundForTip()
	{
		Log.d(THIS_FILE, "soundForTip");
//		CallAgent callAgent = (CallAgent)groupCallOperator;
//		CallOperator operator = callAgent.getOperator( );
	}

	private boolean canPttBtnDown(int callState,int SpeakState,int transState,int recvState)
	{
		if(CallConst.CallState_InitState == callState
				|| CallConst.FloorState_NoPermission == SpeakState
				|| CallConst.VideoTransState_NoPermission == transState
				|| CallConst.VideoRecvState_NoPermission == recvState)
		{
			return true;
		}
		return false;
	}

	private boolean isActiveCall(int callState)
	{
		if(CallConst.CallState_Activite == callState)
		{
			return true;
		}
		return false;
	}

//	@Override
//	public void incomingCall(String callKey,AddrInfo incomingInfo)
//	{
//		Log.d(THIS_FILE,"incomingCall callKey = " + callKey + " this.curCallKey = " + this.curCallKey + " incomingInfo = " + incomingInfo);
//		this.curCallKey = callKey;
////		product.setCurGroupInfo(incomingInfo);
//		if(desc != null)
//		{
//			desc.incomingCall(callKey,incomingInfo);
//		}
//	}
	@Override
	public void updateCallState(String callKey, int callState,boolean isHasVideo) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE,"updateCallState callKey = " + callKey + " callState = " + callState);
		this.curCallKey = callKey;
		this.callState = callState;
		if(callListener != null)
		{
			callListener.updateCallState(callKey, callState,isHasVideo);
		}
		if(desc != null)
		{
			if(callState == CallConst.CallState_InitState
				|| callState == CallConst.CallState_WattingState)
			{
				desc.updateCallInfoDesc(callKey, callState,"");
				desc.updateCallUser(callKey, false,"");
			}
			else
			{
				AddrInfo curInfo = product.getCurGroupInfo();
				if(curInfo != null)
				{
					desc.updateCallUser(callKey, true,curInfo.getName());
				}
				String value = callDescInfo.getErrDesc(callState);
				if(callState == CallConst.CallState_InitState
						|| callState == CallConst.CallState_WattingState
						|| callState == CallConst.CallState_ConnectingState
						|| callState == CallConst.CallState_DisconnectingState)
				{
					desc.updateCallInfoDesc(callKey, callState,value);
				}
				else if(callState == CallConst.CallState_Activite)
				{
					value = callDescInfo.getErrDesc(CallConst.FloorState_NoPermission);
					desc.updateCallInfoDesc(callKey, callState,value);
				}
			}
		}
	}
	@Override
	public void updateVideoEvent(String callKey)
	{}
	@Override
	public void updateGroupCallMemberInfoList(List<GroupCallMemberInfo> groupCallMemberInfoList)
	{
	}

	@Override
	public void updateSpeakState(String callKey, int floorState) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE,"updateSpeakState callKey = " + callKey + " floorState = " + floorState);
		this.curCallKey = callKey;
		this.floorState = floorState;
		if(floorListener != null)
		{
			floorListener.updateSpeakState(callKey, floorState);
		}
		if(desc != null)
		{
			String value = callDescInfo.getErrDesc(floorState);
			if(floorState == CallConst.FloorState_Null
					|| floorState == CallConst.FloorState_NoPermission
					|| floorState == CallConst.FloorState_PendingRequset
					|| floorState == CallConst.FloorState_Permission
					|| floorState == CallConst.FloorState_PendingRelease)
			{
				desc.updateFloorInfoDesc(callKey, floorState,value);
			}
		}
		else
		{
			Log.d(THIS_FILE,"updateSpeakState desc = null");
		}
		if(desc != null)
		{
			desc.updateFloorState(callKey,floorState,true);
		}
	}


	@Override
	public void updateSpeakPermissionInfo(String callKey, int floorState,AddrInfo info) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE,"updateSpeakPermissionInfo callKey = " + callKey + " floorState = " + floorState + " info = " + info);
		this.curCallKey = callKey;
		this.floorAddrinfo = info;
		if(floorListener != null)
		{
			floorListener.updateSpeakPermissionInfo(callKey, floorState,info);
		}
		if(desc != null)
		{
			if(info != null)
			{
				desc.updateFloorState(callKey, floorState,false);
				String takenStr = callDescInfo.getTakenKey();
				desc.updateFloorInfoDesc(callKey,floorState,info.getName() + takenStr);
			}
			else
			{
				desc.updateFloorState(callKey, floorState,true);
				String value = callDescInfo.getErrDesc(CallConst.FloorState_NoPermission);
				desc.updateFloorInfoDesc(callKey,floorState,value);
			}
		}
		else
		{
			Log.d(THIS_FILE,"updateSpeakPermissionInfo desc = null");
		}
	}


	@Override
	public void notifyErrStr(String callKey, String errStr) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE,"notifyErrStr callKey = " + callKey + " errStr = " + errStr);
	}


	@Override
	public void updateMicMute(String callKey,final boolean isMicMute)
	{

	}
	@Override
	public void updateMediaStatus(String callKey, int mediaStatus)
	{}

	@Override
	public void updateCallDuration(String callKey, String secStr) {
		// TODO Auto-generated method stub
		Log.d(THIS_FILE,"updateCallDuration callKey = " + callKey + " secStr = " + secStr);
		this.curCallKey = callKey;
		if(callListener != null)
		{
			callListener.updateCallDuration(callKey, secStr);
		}
		if(desc != null)
		{
			desc.updateSecDesc(callKey, secStr);
		}
		else
		{
			Log.d(THIS_FILE,"updateCallDuration desc = null");
		}
	}

	private void updateCallDescByCallInfo(String callKey,HalfCallInfo callInfo)
	{
		if(callInfo != null)
		{
			this.callState = callInfo.getCallState();
			this.updateCallState(callKey, callState,false);
			this.floorState = callInfo.getFloorState();
			this.updateSpeakState(callKey, floorState);
			this.floorAddrinfo = callInfo.getFloorUserInfo();
			this.updateSpeakPermissionInfo(callKey, floorState,floorAddrinfo);
		}
		else
		{
			this.updateCallState(callKey, CallConst.CallState_InitState,false);
		}
	}
	@Override
	public void networkChange(boolean isConnect) {
		Log.i(THIS_FILE, "networkChange isConnect="+isConnect);
		if(product !=null){
			product.networkChange(isConnect);
		}
	}

	@Override
	public void onReqLogin(LoginInfoStruct data) {
		Log.i(THIS_FILE, "onReqLogin");
	}

	@Override
	public void onReqConnectWatch(String macAddress,int opt) {
		Log.i(THIS_FILE, "onReqConnectWatch  macAddress=" +macAddress+",opt ="+opt);
	}

	@Override
	public void onIgstStatusAck() {

	}

	public void onVol(int keyCode)
	{
		CallOperator op = product.getSuperCallOperator().getCallOperator();
		switch (keyCode) {
			case KeyEvent.KEYCODE_VOLUME_UP:
				op.onVol(AppRTCAudioManager.Vol_Type_Up);
				break;
			case KeyEvent.KEYCODE_VOLUME_DOWN:
				op.onVol(AppRTCAudioManager.Vol_Type_Down);
				break;
			case KeyEvent.KEYCODE_VOLUME_MUTE:

				break;
		}
//		ISuperCallOperator op = product.getSuperCallOperator();
//		switch (keyCode) {
//			case KeyEvent.KEYCODE_VOLUME_UP:
//				op.doUserAction(curCallKey, CallConst.BtnAction_Vol_Up);
//				break;
//			case KeyEvent.KEYCODE_VOLUME_DOWN:
//				op.doUserAction(curCallKey, CallConst.BtnAction_Vol_Down);
//				break;
//			case KeyEvent.KEYCODE_VOLUME_MUTE:
//
//				break;
//		}
	}

	@Override
	public void onCloseApplication(int isopen) {
		Log.i(THIS_FILE, "onCloseApplication isopen="+isopen);
	}

	@Override
	public void onReqConnectWatchStatus() {
		//��ȡ��ǰ�������mac��name
		Log.i(THIS_FILE, "getConnectWatchInfo");
	}

	//	@Override
//	public void onDisplayVideo(int callId, boolean show) {
//		// TODO Auto-generated method stub
//
//	}
	public void setGroup(boolean isGroup) {
		Log.d(THIS_FILE,"pttBtnAction setGroup = " + isGroup);
		this.isGroup = isGroup;
	}
	public AddrInfo getCurGroupInfo()
	{
		if(product != null)
		{
			return product.getCurGroupInfo();
		}
		return null;
	}

	public boolean hasActivieGroupVideoCall()
	{
		Log.d(THIS_FILE,"hasActivieGroupVideoCall" );
		List<HalfCallInfo> halfCallInfoList = product.getSuperCallOperator().getGroupCallInfoList();
		Log.d(THIS_FILE,"hasActivieGroupVideoCall halfCallInfoList = " + halfCallInfoList);
		if(halfCallInfoList != null
				&& halfCallInfoList.size() > 0)
		{
			for (Iterator<HalfCallInfo> itor = halfCallInfoList.iterator();itor.hasNext();)
			{
				HalfCallInfo info = itor.next();
				Log.d(THIS_FILE,"pttBtnAction info = " + info);
				if(!info.isHasVideo()
						&& info.getCallState() == CallConst.CallState_Activite)
				{
					return true;
				}
			}
		}
		return false;
	}

	public HalfCallInfo getHalfCallInfo(String callKey)
	{
		List<HalfCallInfo> halfCallInfoList = product.getSuperCallOperator().getGroupCallInfoList();
		Log.d(THIS_FILE,"getHalfCallInfo halfCallInfoList = " + halfCallInfoList);
		if(halfCallInfoList != null
				&& halfCallInfoList.size() > 0)
		{
			for (Iterator<HalfCallInfo> itor = halfCallInfoList.iterator();itor.hasNext();)
			{
				HalfCallInfo info = itor.next();
				CallAttr callAttr = info.getCallAttr();
				Log.d(THIS_FILE,"pttBtnAction info = " + info);
				if(!info.isHasVideo()
						&& info.getCallState() == CallConst.CallState_Activite
				&& callAttr != null
				&& callAttr.getCallKey() != null
				&& callAttr.getCallKey().equals(callKey))
				{
					return info;
				}
			}
		}
		return null;
	}

	public int getCallState() {
		return callState;
	}

	public int getFloorState() {
		return floorState;
	}
	public AddrInfo getFloorAddrinfo() {
		return floorAddrinfo;
	}
}
